using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class HL_Admin_Admin_Email : Admin, IRequiresSessionState
{
	protected RadioButtonList SendtoSelect;
	protected CheckBoxList SendGroup;
	protected TextBox SendUserList;
	protected HtmlAnchor ShowUserList;
	protected TextBox SendEmailList;
	protected TextBox EmailTitle;
	protected RequiredFieldValidator RequiredFieldValidator1;
	protected TextBox EmailContent;
	protected TextBox EmailSender;
	protected RequiredFieldValidator RequiredFieldValidator3;
	protected TextBox SenderEmailAddress;
	protected RequiredFieldValidator RequiredFieldValidator4;
	protected Button SendEmailButton;
	protected HtmlTable Sendlist;
	protected Literal EmailSuccess;
	protected Literal EmailFail;
	protected Panel SendResult;
	protected ListBox Group;
	protected Button Button1;
	protected HtmlTable MailList;
	protected Literal result;
	protected HyperLink txtUrl;
	protected Panel ShowResult;
	protected TextBox SearchKey;
	protected DropDownList SearchType;
	protected Button SearchEmailButton;
	protected GridView GridView1;
	protected Panel SearchEmail;
	public string ErrMssage = "";
	private UserBll _user;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected UserBll m_User
	{
		get
		{
			UserBll user;
			if (this._user == null)
			{
				this._user = new UserBll(this.conn);
				user = this._user;
			}
			else
			{
				user = this._user;
			}
			return user;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.admin.CheckAdminPower(36))
		{
			base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "Email");
			base.WriteRightErrMsg();
		}
		if (!base.admin.CheckAdminPower(37))
		{
			this.SearchEmailButton.Enabled = (this.Button1.Enabled = (this.SendEmailButton.Enabled = false));
		}
		if (!base.IsPostBack)
		{
			this.EmailSender.Text = this.config["MailServerUserName"].ToString();
			this.SenderEmailAddress.Text = this.config["MailServerFrom"].ToString();
			this.BindGroup();
		}
		string text = Fetch.Get("Action");
		string text2 = text;
		if (text2 != null)
		{
			if (text2 == "Export")
			{
				this.Sendlist.Visible = false;
				this.SearchEmail.Visible = false;
				this.MailList.Visible = true;
				return;
			}
			if (text2 == "SearchEmail")
			{
				this.SearchEmail.Visible = true;
				this.MailList.Visible = false;
				this.Sendlist.Visible = false;
				return;
			}
		}
		this.MailList.Visible = false;
		this.SendtoSelect.Attributes.Add("onClick", "SelectSendType('" + this.SendtoSelect.ClientID + "')");
		this.ShowUserList.Attributes.Add("onClick", "SelectUser('" + this.SendUserList.ClientID + "');");
	}
	protected void BindGroup()
	{
		GroupBll groupBll = new GroupBll(this.conn);
		DataTable groupsNameID = groupBll.GetGroupsNameID();
		this.Group.DataSource = groupsNameID;
		this.Group.DataBind();
		this.SendGroup.DataSource = groupsNameID;
		this.SendGroup.DataBind();
	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		string text = "";
		foreach (ListItem listItem in this.Group.Items)
		{
			if (listItem.Selected)
			{
				text = text + listItem.Value + ",";
			}
		}
		if (text != "")
		{
			text = text.Remove(text.Length - 1);
		}
		string text2 = "";
		DataTable dataTable = new DataTable();
		if (text == "")
		{
			this.WriteErrMsg("", Language.GetAdminMsg("SendEmail/UnGroupSelect"), "");
		}
		string text3 = text;
		if (text3 != null)
		{
			if (text3 == "all")
			{
				dataTable = this.m_User.GetAllUsers();
				goto IL_12B;
			}
		}
		dataTable = this.m_User.GetUserByGroupID(text);
		IL_12B:
		if (dataTable.Rows.Count > 0)
		{
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				if (dataTable.Rows[i]["Email"].ToString() != "")
				{
					string text4 = text2;
					text2 = string.Concat(new string[]
					{
						text4,
						dataTable.Rows[i]["UserName"].ToString(),
						"：",
						dataTable.Rows[i]["Email"].ToString(),
						Environment.NewLine
					});
				}
			}
		}
		else
		{
			string adminMsg = Language.GetAdminMsg("Common/NotData");
		}
		base.Response.ContentType = "application/msword";
		base.Response.Write(text2);
		base.Response.End();
	}
	protected void SendEmailButton_Click(object sender, EventArgs e)
	{
		string selectedValue = this.SendtoSelect.SelectedValue;
		string text = "";
		string text2 = this.SendUserList.Text;
		string text3 = this.SendEmailList.Text;
		string text4 = this.EmailTitle.Text;
		string text5 = this.EmailContent.Text;
		string text6 = this.EmailSender.Text;
		string text7 = this.SenderEmailAddress.Text;
		string text8 = "";
		if (selectedValue == "" || selectedValue == null)
		{
			this.WriteErrMsg("", Language.GetAdminMsg("SendEmail/SendNameTypeErr"), "");
		}
		if (selectedValue == "username" && text2 == "")
		{
			this.WriteErrMsg("", Language.GetAdminMsg("SendEmail/SendNameTypeErr"), "");
		}
		if (selectedValue == "username" && text2 == "")
		{
			this.WriteErrMsg("", Language.GetAdminMsg("SendEmail/SendNameTypeErr"), "");
		}
		if (selectedValue == "email" && text7 == "")
		{
			this.WriteErrMsg("", Language.GetAdminMsg("SendEmail/SendNameTypeErr"), "");
		}
		string smtpserver = this.config["MailSmtpServer"].ToString();
		string mailuser = this.config["MailServerUserName"].ToString();
		string mailpassword = this.config["MailServerPassWord"].ToString();
		string mailFrom = this.config["MailServerFrom"].ToString();
		DataTable dataTable = new DataTable();
		string text9 = selectedValue;
		if (text9 != null)
		{
			if (!(text9 == "all"))
			{
				if (!(text9 == "username"))
				{
					if (!(text9 == "group"))
					{
						if (text9 == "email")
						{
							if (text3.Contains(","))
							{
								string[] array = text3.Split(new char[]
								{
									','
								});
								for (int i = 0; i < array.Length; i++)
								{
									text8 = text8 + array[i] + ",";
								}
							}
							else
							{
								text8 = text3;
							}
							if (text8 != "" && text8.Contains(","))
							{
								text8 = text8.Remove(text8.Length - 1);
							}
						}
					}
					else
					{
						for (int j = 0; j < this.SendGroup.Items.Count; j++)
						{
							if (this.SendGroup.Items[j].Selected)
							{
								text = text + this.SendGroup.Items[j].Value + ",";
							}
						}
						if (text != "")
						{
							text = text.Remove(text.Length - 1);
							dataTable = this.m_User.GetUserByGroupID(text);
							if (dataTable.Rows.Count > 0)
							{
								for (int k = 0; k < dataTable.Rows.Count; k++)
								{
									if (dataTable.Rows[k]["Email"].ToString() != "")
									{
										text8 = text8 + dataTable.Rows[k]["Email"].ToString() + ",";
									}
								}
							}
						}
						if (text8 != "" && text8.Contains(","))
						{
							text8 = text8.Remove(text8.Length - 1);
						}
						dataTable.Dispose();
					}
				}
				else
				{
					string[] array2;
					if (text2.Contains(","))
					{
						array2 = text2.Split(new char[]
						{
							','
						});
					}
					else
					{
						array2 = new string[]
						{
							text2
						};
					}
					for (int l = 0; l < array2.Length; l++)
					{
						dataTable = this.m_User.GetUserByUserName(array2[l]);
						if (dataTable.Rows.Count > 0)
						{
							if (dataTable.Rows[0]["Email"].ToString() != "" && dataTable.Rows[0]["Email"].ToString() != null)
							{
								text8 = text8 + dataTable.Rows[0]["Email"].ToString() + ",";
							}
						}
					}
					if (text8 != "" && text8.Contains(","))
					{
						text8 = text8.Remove(text8.Length - 1);
					}
					dataTable.Dispose();
				}
			}
			else
			{
				dataTable = this.m_User.GetAllUsers();
				if (dataTable.Rows.Count > 0)
				{
					for (int m = 0; m < dataTable.Rows.Count; m++)
					{
						if (dataTable.Rows[m]["Email"].ToString() != "")
						{
							text8 = text8 + dataTable.Rows[m]["Email"].ToString() + ",";
						}
					}
					text8 = text8.Remove(text8.Length - 1);
				}
				dataTable.Dispose();
			}
		}
		if (text8 != "")
		{
			Email email = new Email();
			if (email.SendEmail(smtpserver, mailuser, mailpassword, mailFrom, text5, text8, text4))
			{
				this.EmailSuccess.Visible = true;
			}
			else
			{
				this.EmailFail.Visible = false;
			}
			this.SendResult.Visible = true;
			this.Sendlist.Visible = false;
		}
	}
	protected void SearchEmailButton_Click(object sender, EventArgs e)
	{
		string text = this.SearchKey.Text;
		string selectedValue = this.SearchType.SelectedValue;
		if (text != "-1")
		{
			DataTable dataSource = new DataTable();
			string text2 = selectedValue;
			if (text2 != null)
			{
				if (text2 == "UserName")
				{
					dataSource = this.m_User.GetUserByUserName(text);
					goto IL_91;
				}
				if (text2 == "Email")
				{
					dataSource = this.m_User.GetUserByEmail(text);
					goto IL_91;
				}
			}
			dataSource = null;
			IL_91:
			this.GridView1.DataSource = dataSource;
			this.GridView1.DataBind();
		}
	}
}
